﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;
using System.Windows.Forms;

namespace DataAccess
{
    public static class Connection
    {
        private static SqlConnection con;
        public static string stringConnection;

        public static SqlConnection OpenConnection()
        {
            try
            {
                stringConnection = Common.StaticVariable.ConnectionString;//Common.Connections.GetConnectionString();
                con = new SqlConnection(stringConnection);
                con.Open();
            }
            catch (Exception)
            {
                //con = null;
            }
            return con;
        }

        public static void CloseConnection()
        {
            if (stringConnection == null)
            { return; }
            con.Close();
        }

        public static int Execute(string sql)
        {
            int num = 0;
            try
            {
                con = new SqlConnection(stringConnection);
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                num = cmd.ExecuteNonQuery();
                cmd.Dispose();
                cmd = null;
                con.Close();
            }
            catch (Exception)
            {
                num = 0;
            }
            return num;
        }
    }
}
#endregion
